package cn.dataling.dag.repository;

import cn.dataling.dag.pojo.DagWorkflowNode;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

public class DagWorkflowNodeRepositoryInMemory implements DagWorkflowNodeRepository {

    private final List<DagWorkflowNode> DAG_NODES = new CopyOnWriteArrayList<>();

    @Override
    public Integer save(DagWorkflowNode dagNode) {
        DAG_NODES.add(dagNode);
        return 1;
    }

    @Override
    public Integer updateById(DagWorkflowNode dagNode) {
        List<DagWorkflowNode> waitUpdateNodes = DAG_NODES.stream().filter(e -> e.getId().equals(dagNode.getId())).collect(Collectors.toList());
        for (DagWorkflowNode waitUpdateNode : waitUpdateNodes) {
            waitUpdateNode.setState(dagNode.getState());
        }
        return waitUpdateNodes.size();
    }

    @Override
    public List<DagWorkflowNode> getDagNodeList() {
        return DAG_NODES;
    }
}
